package com.形为型模式.观察者模式;

import java.util.Observable;
import java.util.Observer;

/**
 * 具体目标： 原油期货
 */
class OilFutures extends Observable{
    private float price;

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        super.setChanged();
        super.notifyObservers(price);
        this.price = price;
    }
}
/**
 * 具体观察者 多方
 */
class Bull implements Observer {
    @Override
    public void update(Observable o, Object arg) {
        Float price = ((Float)arg).floatValue();
        if(price>0){
            System.out.println(String.format("油价上涨：%s元，多方高兴了",price));
        }
        else{
            System.out.println(String.format("油价下降：%s元，多方伤心了",price));
        }
    }
}
/**
 * 空方 具体得
 */
class Bear implements  Observer{

    @Override
    public void update(Observable o, Object arg) {
        Float price = ((Float)arg).floatValue();
        if(price>0){
            System.out.println(String.format("油价上涨：%s元，空方伤心了",price));
        }
        else{
            System.out.println(String.format("油价下降：%s元，空方高兴了",price));
        }
    }
}
/**
 * 观察者模式之原油期货
 */
public class ObservOil {
    public static void main(String[] args) {
        OilFutures oil = new OilFutures();
        oil.addObserver(new Bear());
        oil.addObserver(new Bull());
        oil.setPrice(1.1f);
        oil.setPrice(-9f);
    }
}

